import { Router } from 'express';
import { body } from 'express-validator';
import { AuthController } from '@/controllers/auth';
import validateRequest from '@/middleware/validateRequest';
import { authenticateToken as auth } from '@/middleware/auth';

const router = Router();
const authController = new AuthController();

// 注册
router.post(
  '/register',
  [
    body('username').isLength({ min: 1, max: 20 }).withMessage('用户名长度必须在1-20字符之间').trim(),
    body('password').isLength({ min: 6 }).withMessage('密码长度至少6位'),
    body('nickname').optional().isLength({ max: 50 }).withMessage('昵称长度不能超过50字符').trim(),
  ],
  validateRequest,
  authController.register
);

// 登录
router.post(
  '/login',
  [
    body('username').notEmpty().withMessage('用户名不能为空'),
    body('password').notEmpty().withMessage('密码不能为空'),
  ],
  validateRequest,
  authController.login
);

// 刷新令牌
router.post('/refresh', authController.refreshToken);

// 登出
router.post('/logout', auth, authController.logout);

// 获取当前用户信息
router.get('/me', auth, authController.getCurrentUser);

export { router as authRoutes };
export default router;